//
//  UserModel.m
//  Meroad
//
//  Created by 沈煜航 on 2019/2/19.
//  Copyright © 2019 沈煜航. All rights reserved.
//

#import "UserModel.h"

@implementation UserModel

#pragma 字典转模型

-(instancetype)initWithDict:(NSDictionary*)dict{
    if(self=[super init]){
        [self setValuesForKeysWithDictionary:dict];
    }
    if(self.userHeaderImage == nil){
        self.userHeaderImage = [UIImage imageNamed:@"userIcon"];
    }
    
    
    return self;
}

+(instancetype)modelWithDict:(NSDictionary*)dict{
    return [[self alloc] initWithDict:dict];
}


#pragma 定位

-(void)beginGettingLocation{
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 1;  //最小更新距离
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    //开始持续定位
    [self.locationManager startUpdatingLocation];
}


#pragma 代理方法
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location{
    self.location = location;
    [self initialAddress];
}


#pragma 重写属性方法

- (void)initialAddress{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:self.location completionHandler:^(NSArray<CLPlacemark* >* _Nullable placemarks, NSError* _Nullable error){
        NSLog(@"%f  %f",self.location.coordinate.longitude,self.location.coordinate.latitude);
        if(placemarks.count > 0){
            CLPlacemark* placemark = [placemarks objectAtIndex:0];
            if(placemark.administrativeArea != nil){
                self.address = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
            }
            if(placemark.locality != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.locality];
            }
            if(placemark.subLocality != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.subLocality];
            }
            if(placemark.thoroughfare != nil && ![placemark.thoroughfare isEqualToString:placemark.name]){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.thoroughfare];
            }
            if(placemark.subThoroughfare != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.subThoroughfare];
            }
            if(placemark.name != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.name];
            }
            if(placemark.postalCode != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.postalCode];
            }
            
            //NSLog(@"%@",self.address);
        }
    }];
}



@end
